package com.taptech.doufu.db.history;

import java.util.HashMap;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.taptech.doufu.db.history.NovelHirstoryHelper;

public class Read_HirstoryUtil
{

    public static final String TABLE_NAME_HIRSTORY = "readHirstory";
    NovelHirstoryHelper helper;

    public Read_HirstoryUtil(Context context)
    {
        helper = new NovelHirstoryHelper(context);
    }

    // 添加记录
    public void insertHirstory(String novelid, String sectionid)
    {
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL("INSERT INTO " + TABLE_NAME_HIRSTORY + " (novelid,sectionid) VALUES (?,?)", new Object[]{novelid, sectionid});


        db.close();
    }

    // 添加记录
    public void insertCartoonHirstory(String cartoonid, String position)
    {
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL("INSERT INTO " + TABLE_NAME_HIRSTORY + " (cartoonid,episodepos) VALUES (?,?)", new Object[]{cartoonid, position});
        db.close();
    }

    // 更新记录
    public void updateCartoonHirstory(String cartoonid, String position)
    {
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL("UPDATE " + TABLE_NAME_HIRSTORY + " SET episodepos = ? WHERE cartoonid = ? ", new Object[]{position, cartoonid});
        db.close();
    }


    // 查询novelId
    public HashMap<String, String> queryHirstory(String novelid)
    {
        SQLiteDatabase db = helper.getWritableDatabase();
        Cursor c = db.rawQuery("SELECT sectionid FROM " + TABLE_NAME_HIRSTORY + " WHERE novelid = ? ", new String[]{novelid});
        HashMap<String, String> novelIdList = new HashMap<String, String>();
        while (c.moveToNext())
        {
            if (c.getString(0) != null && !c.getString(0).equalsIgnoreCase("null"))
            {
                String sectionid = c.getString(0);
                novelIdList.put(sectionid, sectionid);
            }
        }
        c.close();
        db.close();
        return novelIdList;
    }

    // 查询cartoonId
    public HashMap<String, String> queryCartoonHirstory(String cartoonId)
    {
        HashMap<String, String> novelIdList = null;
        SQLiteDatabase db = helper.getWritableDatabase();
        try
        {
            Cursor c = db.rawQuery("SELECT episodepos FROM " + TABLE_NAME_HIRSTORY + " WHERE cartoonid = ? ", new String[]{cartoonId});
            novelIdList = new HashMap<String, String>();
            while (c.moveToNext())
            {
                if (c.getString(0) != null && !c.getString(0).equalsIgnoreCase("null"))
                {
                    String position = c.getString(0);
                    novelIdList.put(cartoonId, position);
                }
            }
            c.close();
        } catch (Exception e)
        {

        }
        db.close();
        return novelIdList;
    }
}
